import { Tasks } from '@/global/creep-task';
import { profile } from '@/utils/decorators';
import { Logger } from '@/utils/logger';
import { BaseColonyModule } from '../moduleBase';

export class TransportModule extends BaseColonyModule {

    handle() {
        // 运行任务
        const strategy = this.colony.level <= 4 ? this.runEarly : this.runLate;
        this.runCreeps(CreepRole.transporter, transporter => {
            strategy.call(this, transporter);   // note!
        });
    }

    private earlySeekEnergy(transporter: Creep) {
        // 掉落的能量
        const droppedRes = this.colony.capital.find(FIND_DROPPED_RESOURCES, {
            filter: r => r.resourceType === RESOURCE_ENERGY &&
                r.amount >= transporter.pos.getRangeTo(r) * 2   // 数量不能太少
        });
        if (droppedRes[0]) {
            return droppedRes[0];
        }
        // 有能量的罐头
        const sources = this.colony.capital.getSources();
        const validContainer = this.colony.capital.getContainers()
            .find(c => c.store[RESOURCE_ENERGY] > 20 && sources.some(s => s.pos.inRangeTo(c, 2)));
        if (validContainer) {
            return validContainer;
        }
        // 无
        return undefined;
    }

    @profile
    private runEarly(transporter: Creep) {
        // 早期爬是一个简单的状态机，流程为找掉落的能量 / container 里的能量，放到
        // spawn / extension / tower / storage 里。此时还没有 fastfill 快速
        // 填充的功能
        if (transporter.store[RESOURCE_ENERGY] > 0) {
            // 找地方放
            const capital = this.colony.capital;
            const spawn = capital.getSpawns()[0];
            if (!spawn) {
                transporter.say('😨我家没了');
                return;
            }
            if (spawn.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
                transporter.addTask(Tasks.transfer(spawn, RESOURCE_ENERGY));
                transporter.say('🚚>spawn');
                return;
            }

            const extension = capital.getExtensions().find(e => {
                return e.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && e.targetedBy.length === 0;
            });
            if (extension) {
                transporter.addTask(Tasks.transfer(extension, RESOURCE_ENERGY));
                transporter.say('🚚>ext');
                return;
            }

            const tower = capital.getTowers().find(t => {
                return t.store[RESOURCE_ENERGY] <= TOWER_CAPACITY / 2
            });
            if (tower) {
                transporter.addTask(Tasks.transfer(tower, RESOURCE_ENERGY));
                transporter.say('🚚>tower');
                return;
            }

            // 最后放到 container 和 storage 里
            const container = this.colony.structMgr.getHatcheryBatteries().find(c => {
                return c.store.getFreeCapacity() > 0;
            })
            if (container) {
                transporter.addTask(Tasks.transfer(container, RESOURCE_ENERGY));
                transporter.say('🚚>battery');
                return;
            }

            if (capital.getStorage()) { // 大罐头一般不会存满
                transporter.addTask(Tasks.transfer(capital.getStorage(), RESOURCE_ENERGY));
                transporter.say('🚚>storage');
                return;
            }

            transporter.say('🚚>?');
        }
        else {
            // 找能量
            const from = this.earlySeekEnergy(transporter);
            if (!from) {
                transporter.say('😆没活');
                return;
            }
            if ('amount' in from) {
                transporter.addTask(Tasks.pickup(from));
            }
            else {
                transporter.addTask(Tasks.withdraw(from, RESOURCE_ENERGY));
            }
        }
    }

    @profile
    private runLate(transporter: Creep) {

    }

}